package com.wxd.woff.table;


import com.wxd.woff.AbstractOTFDataStream;
import com.wxd.woff.AbstractWoffTable;
import com.wxd.woff.ByteBufferOTFDataStream;
import com.wxd.woff.WoffFont;

import java.io.IOException;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wangxindong
 * @date: 2018/9/1-22:39
 */
public class HeaderTable extends AbstractWoffTable {

    public static final String TAG = "head";

    private int majorVersion;
    private int minorVersion;
    private int fontRevision;
    private long checkSumAdjustment;
    private long magicNumber;
    private int flags;
    private int unitsPerEm;
    private Date created;
    private Date modified;
    private short xMin;
    private short yMin;
    private short xMax;
    private short yMax;
    private int macStyle;
    private int lowestRecPPEM;
    private short fontDirectionHint;
    private short indexToLocFormat;
    private short glyphDataFormat;

    public HeaderTable(WoffFont woffFont) {
        super(woffFont);
    }

    @Override
    public void read(WoffFont woffFont, AbstractOTFDataStream stream) throws IOException {
        stream.seek(offset);
        byte[] fontData = stream.read(compLength);
        byte[] inflateFont = inflateFontData(fontData);
        AbstractOTFDataStream data = new ByteBufferOTFDataStream(inflateFont);
        majorVersion = data.readUnsignedShort();
        minorVersion = data.readUnsignedShort();
        fontRevision = data.readInt();
        checkSumAdjustment = data.readUnsignedInt();
        magicNumber = data.readUnsignedInt();
        flags = data.readUnsignedShort();
        unitsPerEm = data.readUnsignedShort();
        created = new Date(data.readLong());
        modified = new Date(data.readLong());
        xMin = data.readSignedShort();
        yMin = data.readSignedShort();
        xMax = data.readSignedShort();
        yMax = data.readSignedShort();
        macStyle = data.readUnsignedShort();
        lowestRecPPEM = data.readUnsignedShort();
        fontDirectionHint = data.readSignedShort();
        indexToLocFormat = data.readSignedShort();
        glyphDataFormat = data.readSignedShort();
    }

    public static String getTAG() {
        return TAG;
    }

    public int getFontRevision() {
        return fontRevision;
    }

    public void setFontRevision(int fontRevision) {
        this.fontRevision = fontRevision;
    }

    public long getCheckSumAdjustment() {
        return checkSumAdjustment;
    }

    public void setCheckSumAdjustment(long checkSumAdjustment) {
        this.checkSumAdjustment = checkSumAdjustment;
    }

    public long getMagicNumber() {
        return magicNumber;
    }

    public void setMagicNumber(long magicNumber) {
        this.magicNumber = magicNumber;
    }

    public int getFlags() {
        return flags;
    }

    public void setFlags(int flags) {
        this.flags = flags;
    }

    public int getUnitsPerEm() {
        return unitsPerEm;
    }

    public void setUnitsPerEm(int unitsPerEm) {
        this.unitsPerEm = unitsPerEm;
    }

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getModified() {
        return modified;
    }

    public void setModified(Date modified) {
        this.modified = modified;
    }

    public int getMajorVersion() {
        return majorVersion;
    }

    public void setMajorVersion(int majorVersion) {
        this.majorVersion = majorVersion;
    }

    public int getMinorVersion() {
        return minorVersion;
    }

    public void setMinorVersion(int minorVersion) {
        this.minorVersion = minorVersion;
    }

    public short getxMin() {
        return xMin;
    }

    public void setxMin(short xMin) {
        this.xMin = xMin;
    }

    public short getyMin() {
        return yMin;
    }

    public void setyMin(short yMin) {
        this.yMin = yMin;
    }

    public short getxMax() {
        return xMax;
    }

    public void setxMax(short xMax) {
        this.xMax = xMax;
    }

    public short getyMax() {
        return yMax;
    }

    public void setyMax(short yMax) {
        this.yMax = yMax;
    }

    public int getMacStyle() {
        return macStyle;
    }

    public void setMacStyle(int macStyle) {
        this.macStyle = macStyle;
    }

    public int getLowestRecPPEM() {
        return lowestRecPPEM;
    }

    public void setLowestRecPPEM(int lowestRecPPEM) {
        this.lowestRecPPEM = lowestRecPPEM;
    }

    public short getFontDirectionHint() {
        return fontDirectionHint;
    }

    public void setFontDirectionHint(short fontDirectionHint) {
        this.fontDirectionHint = fontDirectionHint;
    }

    public short getIndexToLocFormat() {
        return indexToLocFormat;
    }

    public void setIndexToLocFormat(short indexToLocFormat) {
        this.indexToLocFormat = indexToLocFormat;
    }

    public short getGlyphDataFormat() {
        return glyphDataFormat;
    }

    public void setGlyphDataFormat(short glyphDataFormat) {
        this.glyphDataFormat = glyphDataFormat;
    }

    @Override
    public String toString() {
        return "AbstractWoffTable{" +
                "tag='" + tag + '\'' +
                ", offset=" + offset +
                ", compLength=" + compLength +
                ", origLength=" + origLength +
                ", origChecksum=" + origChecksum +
                ", majorVersion=" + majorVersion +
                ", minorVersion=" + minorVersion +
                ", fontRevision=" + fontRevision +
                ", checkSumAdjustment=" + checkSumAdjustment +
                ", magicNumber=" + magicNumber +
                ", flags=" + flags +
                ", unitsPerEm=" + unitsPerEm +
                ", created=" + created +
                ", modified=" + modified +
                ", xMin=" + xMin +
                ", yMin=" + yMin +
                ", xMax=" + xMax +
                ", yMax=" + yMax +
                ", macStyle=" + macStyle +
                ", lowestRecPPEM=" + lowestRecPPEM +
                ", fontDirectionHint=" + fontDirectionHint +
                ", indexToLocFormat=" + indexToLocFormat +
                ", glyphDataFormat=" + glyphDataFormat +
                '}';
    }
}
